home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 051-075 / disk_074 / control / control.c next >
C/C++ Source or Header  |  1992-05-06  |  8KB  |  301 lines

  1. /*********************************************************************/
  2. /*                                                                   */
  3. /*                     Copyright (c) 1987                            */
  4. /*                    Commodore-Amiga, Inc.                          */
  5. /*                    All rights reserved.                           */
  6. /*                                                                   */
  7. /*                                     */
  8. /*    Unlimited non-comercial use of this program is given,           */
  9. /*    provided this notice is left intact.                 */
  10. /*            andy finkel                     */
  11. /*                                     */
  12. /*********************************************************************/
  13.  
  14.  
  15. #include "exec/types.h" 
  16. #include "exec/memory.h" 
  17. #include "exec/io.h"
  18. #include "exec/libraries.h"
  19. #include "exec/devices.h"
  20. #include "libraries/dos.h"
  21. #include "devices/printer.h"
  22. #include "intuition/intuition.h"
  23. #include "intuition/intuitionbase.h"
  24.  
  25. #define LVOBeginIO   DEV_BEGINIO
  26. #define LVOExpunge   LIB_EXPUNGE
  27.  
  28. #define TASKNAME    "PRT-CONTROL"
  29.  
  30. #define OK 1
  31. #define CANCEL 2
  32. #define NUMBER1 3
  33. #define NUMBER8 10
  34.  
  35. #define BLUE 0 
  36. #define WHITE 1 
  37. #define BLACK 2 
  38. #define RED 3 
  39.  
  40. extern struct Task *FindTask();
  41. extern struct Library *OpenLibrary();
  42. extern struct Window *OpenWindow();
  43. extern ULONG SysBase;
  44.  
  45. extern VOID aBeginIO();  /* The assembler entry */
  46. extern VOID cBeginIO();
  47. extern VOID aExpunge();
  48.  
  49. extern LONG convert();
  50. extern VOID cputs();
  51.  
  52. extern struct Menu menus[];
  53. extern struct Gadget CancelGadget;
  54. extern struct Gadget bgadget[];
  55. extern struct Gadget sGadget[];
  56. extern char buffer1[];
  57. extern char buffer2[];
  58. extern char buffer3[];
  59. extern char buffer4[];
  60. extern char buffer5[];
  61. extern char buffer6[];
  62. extern char buffer7[];
  63. extern char buffer8[];
  64.  
  65. struct IntuitionBase *IntuitionBase=NULL;
  66.  
  67. LONG  OldBeginIO, NewBeginIO, OldExpunge, NewExpunge;
  68. char *OldName=NULL;;
  69. struct Device *PrtDevice=NULL;
  70. struct MsgPort replyMsgPort;
  71. int WB;
  72.  
  73. union printerIO {
  74. struct IOStdReq ios;
  75. struct IODRPReq iodrp;
  76. struct IOPrtCmdReq iopc;
  77. } pIO;
  78.  
  79. struct NewWindow nw = {
  80.         0, 10,        /* starting position (left,top) */
  81.         320,160,        /* width, height */
  82.         BLUE,WHITE,     /* detailpen, blockpen */
  83.          /* idcmp flags */
  84.         GADGETUP,
  85.         /* window gadget flags */
  86.         WINDOWDEPTH|WINDOWDRAG|SMART_REFRESH|ACTIVATE,
  87.         &CancelGadget,  /* pointer to 1st user gadget */
  88.         NULL,           /* pointer to user check */
  89.         "Printer Control V1.0", /* title */
  90.         NULL,           /* pointer to window screen */
  91.         NULL,           /* pointer to super bitmap */
  92.         0,0,          /* min width, height */
  93.         0,0,            /* max width, height */
  94.         NULL};
  95.  
  96. main(argc,argv)
  97. int argc;
  98. char *argv[];
  99. {
  100.     struct Task *task;
  101.     LONG exitStatus=TRUE,file;
  102.  
  103.     WB=argc;
  104.     Forbid();
  105.  
  106.     if (task = FindTask(TASKNAME))Signal(task, SIGBREAKF_CTRL_C);
  107.     else {
  108.     if(pOpen()) {
  109.         cputs("Error! Printer in Use\n");
  110.         exit(20);
  111.     }
  112.     OldName = (task = FindTask(0) )->tc_Node.ln_Name;
  113.     FindTask(0)->tc_Node.ln_Name = TASKNAME;
  114.  
  115.        PrtDevice = (struct Device *)pIO.ios.io_Device;
  116.        OldBeginIO = SetFunction(PrtDevice, LVOBeginIO, aBeginIO);
  117.     OldExpunge = SetFunction(PrtDevice, LVOExpunge, aExpunge);
  118.     pClose();
  119.     while (exitStatus) {
  120.         Wait(SIGBREAKF_CTRL_C);
  121.         NewBeginIO=SetFunction(PrtDevice, LVOBeginIO, OldBeginIO);
  122.         NewExpunge=SetFunction(PrtDevice, LVOExpunge, OldExpunge);
  123.         if((NewBeginIO != (LONG)aBeginIO)||(NewExpunge != (LONG)aExpunge)) {
  124.         /* someone else has replaced the vectors ! */
  125.         /* we cannot exit safely now, until the vectors are back */
  126.         SetFunction(PrtDevice, LVOBeginIO, NewBeginIO); /* new ones in */
  127.         SetFunction(PrtDevice, LVOExpunge, NewExpunge); /* new ones in */
  128.         cputs("Error! Can't exit now. Printer Vectors Changed !\n");
  129.         }
  130.         else {
  131.             task->tc_Node.ln_Name = OldName; /* reset the name */
  132.             exitStatus = FALSE;
  133.         }
  134.     }
  135.     }
  136.     Permit();
  137. }
  138.  
  139. VOID
  140. cputs(s)
  141. char *s;
  142. {
  143. LONG file;
  144.             
  145.     if(WB)file=Open("*",MODE_OLDFILE);
  146.     else file=Open("CON:10/10/360/40/Error Window",MODE_NEWFILE);
  147.     if(file) Write(file,s,strlen(s));
  148.     if(!WB)Delay(250); /* wait if Workbench */
  149.     Close(file);
  150. }
  151.  
  152. int
  153. strlen(s)
  154. char *s;
  155. {
  156.    int i = 0;
  157.    while(*s++) i++;
  158.    return(i);
  159. }
  160.  
  161. int
  162. pOpen()
  163. {
  164.     /* open the printer device */
  165.     if (OpenDevice("printer.device", 0, &pIO, 0))return(1);    
  166.  
  167.     /* set up the message port in the I/O request */
  168.     replyMsgPort.mp_Node.ln_Type = NT_MSGPORT;
  169.     replyMsgPort.mp_Flags = 0;
  170.     replyMsgPort.mp_SigBit = 0;
  171.     replyMsgPort.mp_SigTask = 0;
  172.     AddPort(&replyMsgPort);
  173.     pIO.ios.io_Message.mn_ReplyPort = &replyMsgPort;
  174.     return(0);
  175. }
  176.  
  177. pClose()
  178. {
  179.     RemPort(&replyMsgPort);
  180.     CloseDevice(&pIO);
  181. }
  182.  
  183.  
  184.  
  185. /* REMEMBER, THIS CODE RUNS AS PART OF THE PRINTER TASK */
  186. VOID cBeginIO(ior)
  187. union printerIO *ior;
  188. {
  189. struct Window *window;
  190. struct IntuiMessage *message;
  191. struct Gadget *address;
  192.  
  193. int i,exitStatus=TRUE;
  194. ULONG WakeUpBit=0;
  195.  
  196. if(ior->ios.io_Command == PRD_DUMPRPORT) {
  197.     sprintf(buffer1,"0x%lx",ior->iodrp.io_ColorMap); /* fill the buffers */
  198.     sprintf(buffer2,"0x%lx",ior->iodrp.io_Modes);
  199.     sprintf(buffer3,"%ld",ior->iodrp.io_SrcX);
  200.     sprintf(buffer4,"%ld",ior->iodrp.io_SrcY);
  201.     sprintf(buffer5,"%ld",ior->iodrp.io_SrcWidth);
  202.     sprintf(buffer6,"%ld",ior->iodrp.io_SrcHeight);
  203.     sprintf(buffer7,"%ld",ior->iodrp.io_DestCols);
  204.     sprintf(buffer8,"%ld",ior->iodrp.io_DestRows);
  205.  
  206.     for(i=0; i<12; i++) {
  207.     if((ior->iodrp.io_Special)&(1<<i))bgadget[i].Flags |= SELECTED;
  208.     else bgadget[i].Flags &= ~SELECTED;
  209.     }
  210.     if(!(IntuitionBase=
  211.     (struct IntuitionBase *)OpenLibrary("intuition.library",0)))
  212.         exitStatus=0;
  213.     nw.Screen=IntuitionBase->FirstScreen;
  214.     nw.Type = (USHORT)(((struct Screen *)nw.Screen)->Flags & SCREENTYPE);
  215.     if(!(window=OpenWindow(&nw)))exitStatus=0;
  216.     else {
  217.         SetMenuStrip(window, &menus[0]);
  218.         ActivateGadget(&sGadget[0],window,NULL);
  219.     }
  220.     while (exitStatus) {
  221.         WakeUpBit= Wait((1<<window->UserPort->mp_SigBit));
  222.         if(WakeUpBit & (1<<window->UserPort->mp_SigBit)) {
  223.             while(message=(struct IntuiMessage *)GetMsg(window->UserPort)) {
  224.                 switch(message->Class) {
  225.                     case GADGETUP:
  226.             address=message->IAddress;
  227.             exitStatus=DoGadget(address->GadgetID,ior,window);
  228.                         ReplyMsg(message);
  229.                         break;
  230.                    default:
  231.                         ReplyMsg(message);
  232.         }
  233.         }
  234.     }
  235.     }
  236.     if(window) {
  237.     CloseWindow(window);
  238.     ClearMenuStrip(window);
  239.     }
  240.     if(IntuitionBase)CloseLibrary(IntuitionBase);
  241.     for(i=0; i<10000; i++); /* for dpaint */
  242. }
  243. }
  244.  
  245. DoGadget(id,ior,window)
  246. LONG id;
  247. union printerIO *ior;
  248. struct Window *window;
  249. {
  250.  
  251. int i;
  252.  
  253.     if(id == CANCEL) return(0);
  254.     if(id == OK) {
  255.     ior->iodrp.io_ColorMap = (struct ColorMap *)convert(buffer1);
  256.     ior->iodrp.io_Modes = convert(buffer2);
  257.     ior->iodrp.io_SrcX = convert(buffer3);
  258.     ior->iodrp.io_SrcY = convert(buffer4);
  259.     ior->iodrp.io_SrcWidth = convert(buffer5);
  260.     ior->iodrp.io_SrcHeight = convert(buffer6);
  261.     ior->iodrp.io_DestCols = convert(buffer7);
  262.     ior->iodrp.io_DestRows = convert(buffer8);
  263.     ior->iodrp.io_Special = 0;
  264.         for(i=0; i<12; i++) {
  265.         if(bgadget[i].Flags&SELECTED)ior->iodrp.io_Special += (1<<i);
  266.     }
  267.     return(0); /* and exit */
  268.     }
  269.      if( (id>=NUMBER1) && (id<=NUMBER8)) { /* activate next string gadget */
  270.     ActivateGadget(&sGadget[(id-NUMBER1+1)&7],window,NULL);
  271.     }
  272.     return(TRUE); /* keep going */
  273. }
  274.  
  275. LONG
  276. convert(buffer)
  277. UBYTE *buffer;
  278. {
  279. UBYTE *point=buffer,c;
  280. LONG sign=1,mult=10,value=0;    /* assume decimal */
  281.  
  282. while( (c= *point++)) {
  283.      if(c=='x') {
  284.     mult=16; /* oops, hex, start again */
  285.     value=0;
  286.      }
  287.      else if(c=='-') {
  288.     value=0; /* start again */
  289.     sign = -1;
  290.      }
  291.      else if( ((c>='0') && (c <= '9')) || ((c>='A') && (c <= 'F'))) {
  292.         value *= mult;
  293.         value += ( (c>='0') && (c <= '9')) ? c-'0' : c - 'A' + 10;
  294.      }
  295. }
  296. return(value*sign);
  297. }
  298.  
  299.  
  300.  
  301.